#include "linkageActionTable.h"
#include "version.h"
#include "logger.h"
#include "db.h"
#include <string.h>
#include <stdlib.h>

/**
 *  
 */
#define LINKAGE_ACTION_TABLE_NAME "t_linkage_action"
#define SQL_LINKAGE_ACTION_TABLE_CREATE "CREATE TABLE IF NOT EXISTS " LINKAGE_ACTION_TABLE_NAME " (id INT PRIMARY KEY NOT NULL, linkageID INT NOT NULL, deviceID INT NOT NULL, commandID INT NOT NULL, sort INT NOT NULL, delayTime INT NOT NULL, value TEXT)"
#define SQL_LINKAGE_ACTION_TABLE_INSERT(...) "INSERT INTO " LINKAGE_ACTION_TABLE_NAME "(id, linkageID, deviceID, commandID, sort, delayTime, value) VALUES (" #__VA_ARGS__ ")"
#define SQL_LINKAGE_ACTION_TABLE_REPLACE(...) "REPLACE INTO " LINKAGE_ACTION_TABLE_NAME "(id, linkageID, deviceID, commandID, sort, delayTime, value) VALUES (" #__VA_ARGS__ ")"
#define SQL_LINKAGE_ACTION_TABLE_DELETE(id) "DELETE FROM " LINKAGE_ACTION_TABLE_NAME " WHERE id = " #id
#define SQL_LINKAGE_ACTION_TABLE_SELECT(id) "SELECT * FROM " LINKAGE_ACTION_TABLE_NAME " WHERE id = " #id



/*===============================================================*/
/* 联动 */
int createLinkageActionTable(){
    return db_create_table(SQL_LINKAGE_ACTION_TABLE_CREATE);
}

int insertLinkageAction(T_LINKAGE_ACTION* action){
    if(NULL == action){
        log_file_e(APPNAME, "insert linkage action is NULL");
        return -1;
    }
    int ret = db_insert(SQL_LINKAGE_ACTION_TABLE_INSERT(action->id, action->linkageID, action->deviceID, action->commandID, action->sort, action->delayTime, action->value));
    return ret;
}

int replaceLinkageAction(T_LINKAGE_ACTION* action){
    if(NULL == action){
        log_file_e(APPNAME, "replace linkage action is NULL");
        return -1;
    }
    int ret = db_insert(SQL_LINKAGE_ACTION_TABLE_REPLACE(action->id, action->linkageID, action->deviceID, action->commandID, action->sort, action->delayTime, action->value));
    return ret;
}

int deleteLinkageAction(T_LINKAGE_ACTION* action){
    if(NULL == action){
        log_file_e(APPNAME, "delete linkage action is NULL");
        return -1;
    }
    return db_delete(SQL_LINKAGE_ACTION_TABLE_DELETE(action->id));
}
/**
 *  
 */
static int LinkageActionCallback(void *data, int argc, char **argv, char **azColName){
    int i = 0;
    if(NULL == data){
        log_file_e(APPNAME, "LinkageActionCallback data is NULL");
        return -1;
    }
    T_LINKAGE_ACTION_LIST* linkageActionList = (T_LINKAGE_ACTION_LIST*)data;
    if((0 == linkageActionList->count) || (NULL == linkageActionList->head)){
        linkageActionList->head = linkageActionList->tail = (T_LINKAGE_ACTION*)malloc(sizeof(T_LINKAGE_ACTION));
        linkageActionList->head->next = NULL;
        linkageActionList->head->prev = NULL;
    } else {
        linkageActionList->tail->next = (T_LINKAGE_ACTION*)malloc(sizeof(T_LINKAGE_ACTION));
        linkageActionList->tail->next->prev = linkageActionList->tail;
        linkageActionList->tail = linkageActionList->tail->next;
    }
    linkageActionList->count++;
    for(i = 0; i < argc; i++){
        if(0 == strcmp(azColName[i], "id")){
            linkageActionList->tail->id = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "linkageID")){
            linkageActionList->tail->linkageID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "deviceID")){
            linkageActionList->tail->deviceID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "commandID")){
            linkageActionList->tail->commandID = atoll(argv[i]);
        } else if(0 == strcmp(azColName[i], "sort")){
            linkageActionList->tail->sort = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "delayTime")){
            linkageActionList->tail->delayTime = atoi(argv[i]);
        } else if(0 == strcmp(azColName[i], "value")){
            if(NULL == argv[i]){
                linkageActionList->tail->value[0] = '\0';
            } else {
                strcpy(linkageActionList->tail->value, argv[i]);
            }
        }
        log_i("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");
    }
    return linkageActionList->count;
}

int selectLinkageAction(const char* sql, T_LINKAGE_ACTION_LIST* linkageActionList){
    if((NULL == sql) || (1 > strlen(sql)) || (NULL == linkageActionList)){
        log_file_e(APPNAME, "select linkage action sql is NULL");
        return -1;
    }
    return db_select(sql, LinkageActionCallback, linkageActionList);

}
//
void dbLinkageActionInit(){
    createLinkageActionTable();
}
